# shellcheck disable=SC1090
source ~/.bash_profile

# 指定代码的工作区间
if [ -z "${WORKSPACE}" ]; then
  # shellcheck disable=SC2164
  cd ~/
  WORKSPACE='workspace/deploy/jn-cloud'
fi
# 创建工作区间目录
mkdir -p ${WORKSPACE}

# 指定拉取代码的分支
if [ -z "${GIT_BRANCH}" ]; then
  GIT_BRANCH='test'
fi

# maven 编译的环境变量
if [ ${GIT_BRANCH} = 'release' ]; then
  POM_PROFILE='prod_qyd'
else 
  # shellcheck disable=SC2034
  POM_PROFILE=${GIT_BRANCH}
fi

# 指定需要deploy的api项目
if [ -z "${SERVER_SYMBOLS_STR}" ]; then
  SERVER_SYMBOLS=(ums user-act partner-back task data-etl back act-back venus risk crash sms sms-m pay pay-tool mask user product upc marketing match)
else 
  # shellcheck disable=SC2206
  SERVER_SYMBOLS=(${SERVER_SYMBOLS_STR//,/ })
fi

GIT_PREFIX_URL='https://codeup.aliyun.com/jiniutech/jnsk-cloud'
# shellcheck disable=SC2164
cd ${WORKSPACE}
# shellcheck disable=SC2068
for SERVER_SYMBOL in ${SERVER_SYMBOLS[@]}; do
  # 个别命名特殊处理
  if [ "${SERVER_SYMBOL}" = 'ums' ]; then
    SERVER_SYMBOL='ums-user-server'
  fi
  if [ "${SERVER_SYMBOL}" = 'user-act' ]; then
    SERVER_SYMBOL='cloud-user-act-server'
  fi
  if [ "${SERVER_SYMBOL}" = 'partner-back' ]; then
    SERVER_SYMBOL='cloud-partner-back'
  fi
  if [ "${SERVER_SYMBOL}" = 'task' ]; then
    SERVER_SYMBOL='cloud-task-server'
  fi
  if [ "${SERVER_SYMBOL}" = 'data-etl' ]; then
    SERVER_SYMBOL='cloud-data-etl'
  fi
  if [ "${SERVER_SYMBOL}" = 'back' ]; then
    SERVER_SYMBOL='cloud-back'
  fi
  if [ "${SERVER_SYMBOL}" = 'act-back' ]; then
    SERVER_SYMBOL='cloud-act-back'
  fi
  if [ "${SERVER_SYMBOL}" = 'venus' ]; then
    SERVER_SYMBOL='cloud-venus'
  fi
  if [ "${SERVER_SYMBOL}" = 'risk' ]; then
    SERVER_SYMBOL='cloud-risk'
  fi
  if [ "${SERVER_SYMBOL}" = 'crash' ]; then
    SERVER_SYMBOL='cloud-crash'
  fi
  if [ "${SERVER_SYMBOL}" = 'sms' ]; then
    SERVER_SYMBOL='cloud-sms-server'
  fi
  if [ "${SERVER_SYMBOL}" = 'sms-m' ]; then
    SERVER_SYMBOL='sms-middleware-server'
  fi
  if [ "${SERVER_SYMBOL}" = 'pay' ]; then
    SERVER_SYMBOL='cloud-pay-server'
  fi
  if [ "${SERVER_SYMBOL}" = 'pay-tool' ]; then
    SERVER_SYMBOL='cloud-pay-tool-server'
  fi
  if [ "${SERVER_SYMBOL}" = 'mask' ]; then
    SERVER_SYMBOL='cloud-user-mask'
  fi
  if [ "${SERVER_SYMBOL}" = 'user' ]; then
    SERVER_SYMBOL='cloud-user-server'
  fi
  if [ "${SERVER_SYMBOL}" = 'product' ]; then
    SERVER_SYMBOL='cloud-product'
  fi
  if [ "${SERVER_SYMBOL}" = 'upc' ]; then
    SERVER_SYMBOL='cloud-upc'
  fi
  if [ "${SERVER_SYMBOL}" = 'marketing' ]; then
    SERVER_SYMBOL='cloud-marketing'
  fi
  if [ "${SERVER_SYMBOL}" = 'match' ]; then
    SERVER_SYMBOL='cloud-match'
  fi
  
  GIT_URL="${GIT_PREFIX_URL}/${SERVER_SYMBOL}.git"
  echo "${SERVER_SYMBOL}"
  echo "${GIT_URL}"
  if [ ! -d ${SERVER_SYMBOL} ]; then
    git clone -b ${GIT_BRANCH} ${GIT_URL}
  fi

  # shellcheck disable=SC2164
  cd ${SERVER_SYMBOL}
  git checkout ${GIT_BRANCH}
  git pull origin ${GIT_BRANCH}:${GIT_BRANCH}
  mvn clean deploy -DskipTests=true -P${POM_PROFILE} -f api/pom.xml
  # shellcheck disable=SC2103
  cd ..
done